<?php

namespace App\Model;

use EasySwoole\DDL\Enum\Character;
use EasySwoole\DDL\Enum\Engine;
use EasySwoole\ORM\AbstractModel;
use EasySwoole\ORM\Exception\Exception;
use EasySwoole\ORM\Utility\Schema\Table;
use Throwable;

class ActiveModel extends AbstractModel
{
    protected $tableName = 't_active';
    protected $autoTimeStamp = true; // 自动写入create_time和update_time 这两个字段是int

    /**
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getActive($where)
    {
        $where['is_deleted'] = 0;
        return $this->order('id', 'desc')->get($where);
    }

    /**
     * @throws Throwable
     * @throws Exception
     */
    public function addActive($where)
    {
        $this->data($where, false);
        return $this->save();
    }

    public function schemaInfo(bool $isCache = true): Table
    {
        $table = new Table($this->tableName);

        $table->setIfNotExists();
        $table->setTableEngine(Engine::INNODB);
        $table->setTableCharset(Character::UTF8MB4_GENERAL_CI);
        $table->setTableComment('活动表');

        $table->colInt('id')->setIsAutoIncrement()->setIsPrimaryKey(true)->setColumnComment("ID");
        $table->colInt('uid', 25)->setDefaultValue(0)->setIsNotNull()->setColumnComment('用户id');
        $table->colInt('type', 1)->setDefaultValue(0)->setIsNotNull()->setColumnComment('类型：0.免费，1.收费');
        $table->colInt('is_hot', 1)->setDefaultValue(0)->setIsNotNull()->setColumnComment('是否热门：0否，1是');
        $table->colDouble('price', 10,2)->setDefaultValue(0.00)->setColumnComment('价格');
        $table->colInt('join', 5)->setDefaultValue(0)->setIsNotNull()->setColumnComment('参与人数');
        $table->colLongText('content')->setColumnComment('内容');
        $table->colVarChar('image', 255)->setDefaultValue('')->setIsNotNull()->setColumnComment('封面');
        $table->colVarChar('title', 64)->setDefaultValue('')->setIsNotNull()->setColumnComment('标题');
        $table->colVarChar('sponsor', 64)->setDefaultValue('')->setIsNotNull()->setColumnComment('主办方');
        $table->colVarChar('matter', 255)->setDefaultValue('')->setIsNotNull()->setColumnComment('注意事项');
        $table->colVarChar('address', 64)->setDefaultValue('')->setIsNotNull()->setColumnComment('地址');
        $table->colVarChar('contact', 64)->setDefaultValue('')->setIsNotNull()->setColumnComment('联系方式');
        $table->colInt('time', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('活动时间');
        $table->colInt('end_time', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('截止时间');
        $table->colInt('see', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('浏览');
        $table->colInt('person', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('限制人数');
        $table->colTinyInt('is_deleted', 1)->setDefaultValue(0)->setIsNotNull()->setColumnComment('是否删除：0.未删除，1.已删除');
        $table->colInt('create_time', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('创建时间戳');
        $table->colInt('update_time', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('更新时间戳');

        $table->indexNormal('cuid_index', 'uid');
        $table->indexNormal('type_index', 'type');
        $table->indexNormal('join_index', 'join');
        $table->indexNormal('title_index', 'title');
        $table->indexNormal('see_index', 'see');
        $table->indexNormal('person_index', 'person');
        $table->indexNormal('join_index', 'join');

        return $table;
    }
}